/*
 * @lc app=leetcode.cn id=724 lang=javascript
 *
 * [724] 寻找数组的中心下标
 */

// @lc code=start
/**
 * @param {number[]} nums
 * @return {number}
 */
var pivotIndex = function (nums) {
  let sum1 = nums.reduce((a, b) => a + b);
  let sum2 = 0;

  for (const index in nums) {
    const value = nums[index];
    if (sum1 - value === sum2) {
      return Number(index);
    }

    sum1 = sum1 - value;
    sum2 = sum2 + value;
  }
  return -1;
};
// @lc code=end
